package com.cjh.mybatis02.bind;

import com.cjh.mybatis02.session.SqlSession;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @description: 映射器代理类
 * @author: cjh
 * @date: 2023/5/23 - 20:05
 */
@Data
@AllArgsConstructor
public class MapperProxy<T> implements InvocationHandler, Serializable {
    private static final long serialVersionUID = 1L;

    private SqlSession sqlSession ;
    private final Class<T> mapperInterface;


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        如果是调用Object类的方法 就不增强
        if (Object.class.equals(method.getDeclaringClass())){
            return  method.invoke(this,args);
        }else {
            return sqlSession.selectOne(method.getName(),args);
        }

    }
}
